const vm = new Vue({
    el: '#main',
    data: {
        smile: true,
        info: {
            deposit_status: {},
            lease_type: {},
            status: {},
        },
        showGuysDetail: false,
        role: fxj.getSearch().get('role'),
    },
    methods: {
        chatting,
        cancelIndent,
        goHouseResDetail,
        toggleGuysShow,
        nowPay,
        atOnceComment,
        atOnceLandlordComment,
        checkComment,
        delIndent,
        earlyLeave,
        depositDeduct,
    },
});

function apiready() {
    fxj.fixLayout();
    reqIndentDetail();
}

async function reqIndentDetail() {
    const res = await ajax({
        url: `home/order/${vm.role === 'fd' ? 'getLandlordOrderDetail' : 'getTenantOrderDetail'}`,
        data: {
            orderId: fxj.getSearch().get('indentId'),
        },
    });
    res.data.img = fxj.fixImgUrl(res.data.img);
    res.data.start_date = new Date(+(res.data.start_date + '000')).toLocaleDateString();
    res.data.end_date = new Date(+(res.data.end_date + '000')).toLocaleDateString();
    vm.info = res.data;
}

function goHouseResDetail(id) {
    goPage('house_res_detail.html', {
        data: {
            id,
        },
    });
}

function chatting(id) {
    goPage('chat.html', {
        data: {
            landlordId: id,
        },
    });
}

function call(tel) {
    const a = document.createElement('a');
    a.href = `tel:${tel}`;
    a.click();
    call = function () {
        a.click();
    };
}

function toggleGuysShow() {
    vm.showGuysDetail = !vm.showGuysDetail;
}

function cancelIndent() {
    fxj.dialog.alert({
        title: '提示',
        msg: '您确定要取消订单吗?',
        btnLeft: '我要取消订单',
        btnRight: '我点错了',
        leftCallback: async () => {
            const res = await ajax({
                url: 'home/order/cancel',
                data: {
                    orderId: fxj.getSearch().get('indentId'),
                },
            });
            toast(res.msg);
            if (res.err === 0) {
                toast('取消订单成功');
                setTimeout(() => {
                    fxj.closeWin();
                }, 1000);
            }
        },
    });
}

function nowPay(indent) {
    goPage('pay_deposit.html', {
        data: {
            id: indent.oid,
        },
    });
}

function atOnceComment() {
    goPage('comment.html', {
        data: {
            id: fxj.getSearch().get('indentId'),
        },
    });
}

function atOnceLandlordComment() {
    goPage('landlord_comment.html', {
        data: {
            id: fxj.getSearch().get('indentId'),
        },
    });
}

function checkComment() {
    goPage('comment_detail.html', {
        data: {
            id: fxj.getSearch().get('indentId'),
            type: '2',
        },
    });
}

async function delIndent() {
    const close = fxj.dialog.loading({noBg: true});
    const res = await ajax({
        url: 'home/order/deleteOrder',
        data: {
            orderId: fxj.getSearch().get('indentId'),
            role: 2,
        },
    });
    close();
    if (res.err === 0) {
        mui.toast('删除成功');
        setTimeout(() => {
            fxj.closeWin();
        }, 1000);
    }
}

function earlyLeave(indentId) {
    ajax({
        url: 'home/order/cancel',
        data: {
            orderId: indentId,
        },
    }).then(
        res => {
            mui.toast(res.msg);
            setTimeout(() => {
                fxj.closeWin();
            }, 1000);
        },
    );
}

// 申请扣除押金
async function depositDeduct(orderId) {
    const res = await ajax({
        url: 'home/order/depositDeduct',
        data: {
            orderId,
        },
    });
    toast(res.msg, () => {
    }, 3000);
    if (res.err === 0) {
        reqIndentDetail();
    }
}